
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
using System;
using BusinessLayer;
using InfoClasses;
using dpatoolkit.UILayer;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DesignPatternDefinitions;
namespace dpatoolkit.UILayer
{
	/// <summary>
	/// Represents the UI of Simple Aggregate
	/// </summary>
	public class UISingleAggregate : IUIRelation 
	{
		#region Fields
		private static  Pen scPen , ecPen , ncPen;
		#endregion 
		
		#region Constructor
		static UISingleAggregate()
		{
            scPen = new Pen(UIRenderer.SingleAggregateColor, UIRenderer.RelationPenSize);
			scPen.CustomStartCap=PenCapProvider.GetDiamondRepresentation();
            ecPen = new Pen(UIRenderer.SingleAggregateColor, UIRenderer.RelationPenSize);
			ecPen.CustomEndCap=PenCapProvider.GetSingleRepresentation();
            ncPen = new Pen(UIRenderer.SingleAggregateColor, UIRenderer.RelationPenSize);
		}
		
		public UISingleAggregate(string txt , IUIObject _o1, IUIObject _o2,IDesignPatternSubject sub):
			base ( txt,_o1,_o2,null)
		{
			this.subject = sub;
			this.relationContextMenu.MenuItems[editIndex].Click +=new EventHandler(UISingleAggregateEdit_Click);
			this.relationContextMenu.MenuItems[revIndex].Click +=new EventHandler(UISingleAggregateReverse_Click);
			this.relationContextMenu.MenuItems[delIndex].Click +=new EventHandler(UISingleAggregateDel_Click);
            this.relationType = RelationType.SingleAggregate;
		}	

		#endregion

		#region IUIRelation Members

		
		public  override void Draw(Graphics g)
		{
			base.DrawLines(g,scPen,ecPen,ncPen);	
		}

		public  override void SetPariticapants(Rectangle r1, Rectangle r2)
		{
			
		}

		public override ContextMenu ContextMenu
		{
			get
			{
				return this.relationContextMenu;
			}
			set
			{
				//not implemented :P :-P
			}
		}




		#endregion

		private IAggregate ConstructRelationObject()
		{
			IAggregate agg = new AggregateInfo();
			agg.AggregatorClass = this.obj1.UIName;
			agg.AggregatorClassComponent = this.obj1.UIComponent;
			agg.AggregateName = this.RelationName;
			agg.AggregateeClass = this.obj2.UIName;
			agg.AggregateeClassComponent = this.obj2.UIComponent;
			agg.Order = RelationOrder.Single;
			return agg;
		}
		private void UISingleAggregateEdit_Click(object sender, EventArgs e)
		{
			IAggregate oldagg = this.ConstructRelationObject();
			IAggregate newagg = this.ConstructRelationObject();
			RelationEditForm form = new RelationEditForm("Edit Aggregate");
			form.Input= oldagg.AggregateName;
			form.Order = oldagg.Order;
			if( form.ShowDialog()== DialogResult.Cancel)
				return;
			newagg.AggregateName = form.Input;
			newagg.Order = form.Order;
			if(oldagg.Order!=newagg.Order || newagg.AggregateName!=oldagg.AggregateName)
				this.subject.UpdateAggregate(oldagg,newagg);


		}
		private void UISingleAggregateDel_Click(object sender, EventArgs e)
		{
			IAggregate agg = this.ConstructRelationObject();
			this.UnRegisterObserver();
			this.subject.RemoveAggregate(agg);

		}

		private void UISingleAggregateReverse_Click(object sender, EventArgs e)
		{
			IAggregate oldagg = this.ConstructRelationObject();
			if(oldagg.AggregateeClassComponent!=oldagg.AggregatorClassComponent)
			{
				InfoMessage.Show("Sorry currently not supported when exported classes are present!");
				return;
			}

			IAggregate newagg = this.ConstructRelationObject();
			newagg.AggregatorClass = oldagg.AggregateeClass;
			newagg.AggregateeClass = oldagg.AggregatorClass;
			newagg.AggregateeClassComponent = oldagg.AggregatorClassComponent;
			newagg.AggregatorClassComponent = oldagg.AggregateeClassComponent;
			this.subject.UpdateAggregate(oldagg,newagg);
				
		}
	}
}
